var token = localStorage.getItem("pid");
var list = document.getElementById("list");
var kucun = document.getElementById("kucun");
var feikucun = document.getElementById("feikucun");

var sidList = document.getElementById("sidList");
var warehouseList = document.getElementById("warehouseList");

var pageNum = 1;
var itemNum = 20;
var countNum = 0;

var itemID = "";
var itemCode = "";
var funName = "";

var pageflag = "";
var sid = "";
var warehouse = "";
var itemID = "";
var type = ""
var itemCode = "";
var funName = "";
var isShishi = false;

(function(e) {

	mui.init({
		pullRefresh: {
			container: '#list',
			down: {
				style: 'circle',
				callback: pulldownRefresh
			},
			up: {
				auto: true,
				contentrefresh: '正在加载...',
				callback: pullupRefresh
			}
		}
	});

	setPxPerRem();
	mui.plusReady(function() {
		itemID = getQueryString("itemID");
		itemCode = getQueryString("itemCode");
		funName = getQueryString("funName");
		sid = getQueryString("sid");
		isShishi = getQueryString("isShishi");
		warehouse = getQueryString("warehouse");
		type = getQueryString("type");
		pageflag = getQueryString("pageflag");

		if(pageflag == "page_password.html") {
			$("#type").html(type);
			if(type == "出库") {
				funName = "PhoneGetCKSub";
				$("#projectTitle").html("出库明细");
			} else {
				$("#projectTitle").html("入库明细");
				funName = "PhoneGetRKSub";
			}

			hiddenEle(kucun);
			showEle(feikucun);
			console.log(itemCode);
			if(!isEmpty(itemID)) {
				$("#code").html(itemID);
			}
			if(isHaveNet()) {
				getWarehouseInInfo(itemID);
			} else {
				mui.toast("请检查网络连接");
			}
		} else {
			showEle(kucun);
			hiddenEle(feikucun);
		}

	});
})(mui);

var isRefresh = false;

function pulldownRefresh() {
	removeLi("list");
	isRefresh = true;
	pageNum = 1;
	count = 0;
	$("#countNum").html(count);
	isFirst = true;
	if(isHaveNet()) {
		getWarehouse();
	} else {
		mui.toast("无网络连接！");
	}
}

//上拉加载
var isLoad = false;
var isFirst = true;

function pullupRefresh() {
	isLoad = true;
	if(isHaveNet()) {
		getWarehouse();
	} else {
		mui.toast("无网络连接！");
	}
}

var count = 0;

function getWarehouseInInfo(itemId) {
	var pjson = {
		code: itemId
	};

	var jsonData = {
		op: funName,
		pjson: JSON.stringify(pjson)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		if(!isEmpty(data)) {
			$("#Code").html(itemId);
			var rows = data;
			for(var i = 0; i < rows.length; i++) {
				createItem(rows[i], i);
			}
			$("#countNum").html(count);
		} else {
			mui.toast("暂无数据");
		}
	});
}

var keyword = "";
var otherSid = "";
var warehouse_id = "";

var dataList;
var lengthNum = 0;

function getWarehouse() {

	plus.nativeUI.showWaiting();
	if(!isFirst && !isEmpty(dataList)) {
		if(dataList.length > lengthNum) {
			var length = dataList.length;
			if(dataList.length > lengthNum + 20) {
				length = lengthNum + 20;
			}
			for(var i = lengthNum; i < length; i++) {
				createItem(dataList[i], i);
			}
			$("#countNum").html(count);
			lengthNum += itemNum;
			mui('#list').pullRefresh().endPullup(false);
		} else {
			mui('#list').pullRefresh().endPullup(true);
		}
		plus.nativeUI.closeWaiting();
		return;
	}

	var pjson = {
		pid: token,
		"code": keyword
	};
	console.log(JSON.stringify(pjson));
	var jsonData = {
		op: "GetStoreStock",
		pjson: JSON.stringify(pjson)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {
		isFirst = false;
		plus.nativeUI.closeWaiting();
		if(!isEmpty(data)) {
			count = 0;
			dataList = data;
			countNum = dataList.length;
			if(countNum > itemNum) {
				lengthNum = itemNum;
			} else {
				lengthNum = countNum;
			}
			for(var i = 0; i < lengthNum; i++) {
				createItem(dataList[i], i);
			}
			lengthNum += itemNum;
			$("#countNum").html(count);
		} else {
			mui.toast("暂无数据");
		}

		if(isRefresh) {
			mui('#list').pullRefresh().endPulldown(false);
			isRefresh = false;
		}

		if(isLoad) {
			isLoad = false;
			mui('#list').pullRefresh().endPullup(false);
		}
		if(pageNum == 1) {
			mui('#list').pullRefresh().endPullup(false);
		}
		pageNum++;

	});
}

$("#queryBtn").click(function() {
	keyword = $("#searchInput").val().trim();
	if(isEmpty(keyword)) {
		mui.toast("请输入关键字查询");
		return;
	}
	pulldownRefresh();
});

var insertDiv = document.getElementById("insertDiv");

function createItem(row, index) {
	var createLi = document.createElement("li");
	createLi.id = row.id;

	var secondCountStr = row.KC_SecondCount;

	var id = index + 1;
	var SM_Name = row.sm_name;
	var SM_Code = row.sm_nbcode;
	var BOU_Name = row.BOU_Name;
	var SM_Spec = row.sm_spec;
	var SP_Place = row.DateTimes;

	if(pageflag == "page_password.html") {
		if(type == "出库") {
			secondCountStr = row.SOS_SecondCount;
		} else {
			secondCountStr = row.SIS_SecondCount;
		}
	}
	count = Math.round(secondCountStr) + count;
	var secondCount = secondCountStr;

	createLi.innerHTML = '<cite style="width: 1.6rem;">' + id + '</cite>' +
		'<cite style="width: 2.6rem;">' + SM_Name + '</cite>' +
		'<cite style="width: 3.6rem;">' + SM_Code + '</cite>' +
		'<cite style="width: 3.6rem;">'+ BOU_Name +'</cite>' + 
		'<cite style="width: 2.6rem;">' + SM_Spec + '</cite>' +
		'<cite style="width: 2.6rem;">' + secondCount + '</cite>' +
		'<cite style="width: 2.6rem;">' + SP_Place + '</cite>';
	document.body.appendChild(createLi);
	insertBefore(createLi, insertDiv);
}

var sidEle = document.getElementById("sid");
var warehouseEle = document.getElementById("warehouse");
var isGet = false;

function getSidList() {
		token = localStorage.getItem("pid");
	var p = {
		op: "provider_list",
		token: token
	}
	var jsonData = {
		p: JSON.stringify(p)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		isGet = true;
		var htmlStr = '<option value="-1" selected>请选择供应商</option>';
		if(!isEmpty(data)) {
			for(var i = 0; i < data.length; i++) {
				htmlStr += '<option value="' + data[i].o_tid + '">' + data[i].o_name + '</option>'
			}
		}
		sidEle.innerHTML = htmlStr;
	});
}

var currentSid = "";
var currentWarehouse = "";
var isLoad = false;

function getWarehouseList() {
	var p = {
		op: "warehouse_list",
		token: token,
		data: {
			sid: currentSid,
			keyword: ""
		}
	}
	var jsonData = {
		p: JSON.stringify(p)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}
	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		isLoad = true;
		plus.nativeUI.closeWaiting();
		var htmlStr = '<option value="-1" selected="selected">请选择仓库</option>';
		if(!isEmpty(data)) {
			for(var i = 0; i < data.length; i++) {
				htmlStr += '<option value="' + data[i].id + '" >' + data[i].S_Name + '</option>'
			}
		}
		warehouseEle.innerHTML = htmlStr;
	});
}

sidEle.addEventListener('change', function() {
	currentSid = this.value;
	if(currentSid == "-1" || isEmpty(currentSid)) {
		mui.toast("请先选择供销商");
		return;
	}
	warehouseEle.innerHTML = '<option value="-1" selected="selected">请选择仓库</option>';
	getWarehouseList();

}, false);

warehouseEle.addEventListener('change', function() {
	currentWarehouse = this.value;
}, false);